import Config from '../config/config.js';

export default (() => {

    //判断是否为null
    const isNull = (v) => {
        return v == null || v == undefined || v == "" || v == "null";
    }

    //格式化时间
    const formatDateTime = (date) => {
        return [[date.getFullYear(), date.getMonth() + 1, date.getDate()].join("-"), [date.getHours(), date.getMinutes(), date.getSeconds()].join(":")].join(" ");
    };

    /*获取头像路径*/
    const getAvatar = (avatar) => {
        return isNull(avatar) ? "res/img/default_p.png" : (avatar=="system"?"res/img/default_s.png":avatar);
    }

    /*创建Element*/
    const createElement = (elementData) => {
        let element = window.document.createElement(elementData.label||"div");
        if (elementData.props) {
            for (let k in elementData.props) {
                element[k] = elementData.props[k];
            }
        }

        if (elementData.childNodes) {
            elementData.childNodes.forEach(e => {
                let child = createElement(e);
                if(e.tag){
                    element[e.tag] = child;
                }
                element.appendChild(child);
            })
        }
        return element;
    }

    /*根据ID查找element*/
    const getElementByID = (id)=>{
        return window.document.getElementById(id);
    }

    /***常用公共方法集**/
    const tool = {
        //判断null
        isNull: isNull,
        //格式化时间：秒
        formatDateTime: (time) => {
            let date = new Date(time * 1000);
            return formatDateTime(date);
        },
        //当前时间字符串
        nowDateTime: () => {
            let nowDate = new Date();
            return formatDateTime(nowDate);
        },
        getMessTime: (time) => {
            let date = new Date(time * 1000);
            let nowDate = new Date();

            if (date.getFullYear() == nowDate.getFullYear() && date.getMonth() == nowDate.getMonth()) {
                if (date.getDate() == nowDate.getDate()) {
                    return date.getHours() + ":" + date.getMinutes();
                } else if (date.getDate() + 1 == nowDate.getDate()) {
                    return "昨天"
                } else if (date.getDate() + 2 == nowDate.getDate()) {
                    return "前天"
                } else {
                    return date.getMonth() + 1 + "-" + date.getDate();
                }

            } else {
                return date.getMonth() + 1 + "-" + date.getDate();
            }

        },
        addHandle: function(element,type,handle) {
            if(element.addEventListener) {
                element.addEventListener(type,handle);
            }else if(element.attachEvent) {
                element.attachEvent("on"+type,handle);
            }else {
                element["on"+type] = handle;
            }
        },
        removeHandle: function(element,type,handle) {
            if(element.removeEventListener) {
                element.removeEventListener(type,handle);
            }else if(element.detachEvent) {
                element.detachEvent("on"+type,handle);
            }else {
                element["on"+type] = null;
            }
        },
        getFileInfo:(fileUrl) => {
            let pos = fileUrl.lastIndexOf("\\");
            let filename = fileUrl.substring(pos + 1);
            let fileextname = filename.substring(filename.lastIndexOf(".") + 1, filename.length).toUpperCase();
            let icon;
            switch (fileextname) {
                case "DOC":
                case "DOCX":
                    icon = "doc.png";
                    break;
                case "XLS":
                case "XLSX":
                    icon = "xls.png";
                    break;
                case "PPT":
                case "PPTX":
                    icon = "ppt.png";
                    break;
                case "ZIP":
                case "RAR":
                    icon = "zip.png";
                    break;
                case "EXE":
                    icon = "exe.png";
                    break;
                case "MP3":
                    icon = "mp3.png";
                    break;
                case "MP4":
                    icon = "mp4.png";
                    break;
                case "PNG":
                case "GIF":
                case "JPG":
                case "JPEG":
                    icon = "png.png";
                    break;
                default:
                    icon = "other.png";
                    break
            }

            return {
                icon: "res/img/icon/" + icon,
                name: filename,
                extname: fileextname
            };
        },
        /*获取头像路径*/
        getAvatar: getAvatar,
        /*创建Element*/
        createElement:createElement,
        /*根据ID查找element*/
        getElementByID:getElementByID
    }

    /***日志类**/
    let log = (module) => {
        module = module ? module : "";
        const Log_Level = {
            INFO: "INFO",
            DEBUG: "DEBUG",
            WARN: "WARN",
            ERROR: "ERROR"
        }
        //打印
        let print = (level, info, data) => {
            let datetime = tool.nowDateTime();
            info = [module, datetime, info].join(" ");
            switch (level) {
                case Log_Level.INFO:
                    console.info(info, data);
                    break;
                case Log_Level.DEBUG:
                    console.debug(info, data);
                    break;
                case Log_Level.WARN:
                    console.warn(info, data);
                    break;
                case Log_Level.ERROR:
                    console.error(info, data);
                    break;
                default:
                    break;
            }
        };

        return {
            info: (info, data) => {
                if (Config.LogLevel < 2 ) {
                  print(Log_Level.INFO, info, data);
                }
            },
            debug: (info, data) => {
                if (Config.LogLevel < 1 ) {
                print(Log_Level.DEBUG, info, data);
                }
            },
            warn: (info, data) => {
                if (Config.LogLevel < 3 ) {
                print(Log_Level.WARN, info, data);
                }
            },
            error: (info, data) => {
                if (Config.LogLevel < 4) {
                    print(Log_Level.ERROR, info, data);
                }
            }
        }
    }

    //简单缓存
    const cache = {
        userInfo:{},
        myProfile:{}
    }
    return {
        cache:cache,
        tool: tool,
        log: log
    }
})();


